/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package museo;

/**
 *
 * @author Raul
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.swing.table.DefaultTableModel;


public class ConexionTopic implements Runnable{

    String server;
    String name;
    String topic;
    String cola;
    Connection connection=null;
    Destination destinationTopic=null;
    Destination destinationCola=null;
    Session session=null;
    InputStreamReader inputStreamReader = null;
    char answer = '\0';
    //DefaultTableModel tabla;
    
    /*public ConexionTopic(String server, String name,String topic,String cola, DefaultTableModel lt){
        this.server = server;
        this.name = name;
        this.topic = topic;
        this.tabla = lt;
        this.cola = cola;
    }*/

    public ConexionTopic(String server, String name, String topic, String cola){
        this.server = server;
        this.name = name;
        this.topic = topic;
        this.cola = cola;
    }

    public boolean Conectar(){

        try{
            // Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://"+server);
            connectionFactory.setClientID(name);

            // Create a Connection
            connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // Create the destination (Topic or Queue)
            destinationTopic = session.createTopic(topic);
            destinationCola = session.createQueue(cola);

        } catch (JMSException ex) {
            Logger.getLogger(SubastaView.class.getName()).log(Level.SEVERE, null, ex);
        }

        return connection!=null;
    }

    public void Publicar(String msg) {
        try {
            // Create a MessageProducer from the Session to the Topic or Queue
            MessageProducer producer = session.createProducer(destinationCola);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // Create a messages
            TextMessage message = session.createTextMessage(msg);
            // Tell the producer to send the message
            System.out.println("Sent message: " + msg);
            producer.send(message);
        } catch (JMSException ex) {
            Logger.getLogger(ConexionTopic.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //Consumir
    public void run(){
        try {

            MessageConsumer mc = session.createDurableSubscriber((Topic) destinationTopic, name);
            TextListener listener = new TextListener() {

                public void textValueChanged(TextEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
            
            mc.setMessageListener((MessageListener) listener);
            

            inputStreamReader = new InputStreamReader(System.in);
            while (true) {
                try {
                    answer = (char) inputStreamReader.read();
                    System.out.print(answer);
                } catch (IOException e) {
                    System.out.println("I/O exception: "+ e.toString());
                }
            }

        } catch (JMSException e) {
            System.out.println("Exception occurred: " +e.toString());
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }

    public void Cerrar(){
        try {
            session.close();
            connection.close();
            System.out.println("SALIMOS");
        } catch (JMSException ex) {
            Logger.getLogger(ConexionTopic.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}



